package com.woniuxy.microutilsserver.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author unreleased
 * @date 2021/1/29 12:16
 */
@Configuration
public class RabbitMqConfigure {
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange", true, false);
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("deadLetterExchange", true, false);
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue("deadLetterQueue", true);
    }

    @Bean
    public Queue messageQueue() {
        Map<String, Object> args = new HashMap<>(2);
        //x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadLetterExchange");
        //x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "dlkey");
        //durable – true if we are declaring a durable queue (the queue will survive a server restart)
        //exclusive – true if we are declaring an exclusive queue (the queue will only be used by the declarer's connection)
        //autoDelete – true if the server should delete the queue when it is no longer in use
        //arguments – the arguments used to declare the queue
        return new Queue("messageQueue", true, false, false, args);
        //利用建造者模创建一个持久化的队列
        //return QueueBuilder.durable("messageQueue").withArguments(args).build();
    }

    @Bean
    public Binding bindingFanoutExchange() {
        return BindingBuilder.bind(messageQueue()).to(fanoutExchange());
    }


    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("dlkey");
    }
}
